<template>
    <view class="page-wrap">
        <Position :info="workDetail" :type="type" :remark="remark" @changeRemark="changeRemark"></Position>

        <view class="content">
            <view class="left hiddenScroll">
                <view class="item" @tap="handClick(index)" :class="{ active: index == current }"
                    v-for="(item, index) in list" :key="index">
                    <view class="txt">{{ item.title }}</view>
                </view>
            </view>
            <view class="right" :class="[isDetail ? 'nopadding' : '']">
                <safe v-if="type == '3'" :intoIndex="intoIndex" :uploadParams="uploadParams" :defaultValue="defaultValue"
                    :list="list" @getForm="getForm"></safe>
                <basePoint v-if="type == '2'" :intoIndex="intoIndex" :uploadParams="uploadParams"
                    :defaultValue="defaultValue" :list="list" @getForm="getForm"></basePoint>
                <network v-if="type == '1'" :intoIndex="intoIndex" :uploadParams="uploadParams" :defaultValue="defaultValue"
                    :list="list" @getForm="getForm"></network>
            </view>
        </view>

        <view class="btns" v-if="!isDetail">
            <view class="btn save" @tap="submit(1)">存草稿</view>
            <view class="btn sub" @tap="submit(0)">确认提交</view>
        </view>
    </view>
</template>

<script>

import network from './components/network/index.vue'
import basePoint from './components/basePoint/index.vue'
import safe from './components/safe/index.vue'
import { patrolAdd, getWorkDetail, getWorkInfo } from '@/api'
export default {
    components: { network, basePoint, safe },
    computed: {
        list() {
            return this.options[this.type]
        },
        workType() {
            let map = {
                1: "网点",
                3: "安防",
                2: "基站",
            }
            return map[this.type]
        }
    },
    data() {
        return {
            isDetail: '',
            type: '',
            current: '0',
            intoIndex: '',
            defaultValue: null,
            form: null,
            workInfo: null,
            workDetail: null,
            remark: '',
            options: {
                1: [
                    {
                        title: '消防供电配电',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '火灾报警系统',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '消防供水',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '消火栓',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '自动喷水系统',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '气体灭火系统',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '机械加压送风系统',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '机械排烟系统',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '应急照明',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '疏散指示标志',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '应急广播系统',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '消防专用电话',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '防火隔离',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '消防电梯',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '灭火器',
                        needFile: true,
                        fileList: [],
                    },
                ],
                2: [
                    {
                        title: '主控制器',
                        key: '主控制器',
                        needFile: true,
                        fileList: [],
                    },

                    {
                        title: '气体储瓶',
                        key: '气体储瓶',
                        needFile: true,
                        fileList: [],
                    },

                    {
                        title: '应急照明灯',
                        key: '应急照明灯',
                        needFile: true,
                        fileList: [],
                    },

                    {
                        title: '手提灭火器',
                        key: '手提灭火器',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '其他设置设备数量',
                        key: '其他设置设备数量',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '六氮丙烷',
                        key: '六氮丙烷',
                        needFile: true,
                        fileList: [],
                    },
                ],
                3: [
                    {
                        title: '视频监控系统',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '报警系统',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '门禁系统',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '停车场系统',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '电子巡逻更系统',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '访客系统',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '消费系统',
                        needFile: true,
                        fileList: [],
                    },
                    {
                        title: '人脸识别系统',
                        needFile: true,
                        fileList: [],
                    },
                ]
            },
            uploadParams: {}
        }
    },
    onLoad(options) {
        console.log(options)
        const { type, id, pid, mid, isDetail } = options
        this.type = type
        this.isDetail = isDetail
        this.workInfo = {
            type, id, pid, mid
        }
        this.uploadParams = {
            type, id, pid, mid,
            wid: this.wid
        }
        this.getWorkInfo(this.workInfo)
        this.getWorkDetail(this.workInfo)
    },
    onShow(){
        this.getWorkInfo(this.workInfo) 
    },
    methods: {
        async getWorkDetail(params) {
            if (params.id != '0') {
                let res = await getWorkDetail({
                    id: params.id,
                    wid: this.wid,
                    type: '1'
                })
                if (res.formdata) {
                    this.defaultValue = JSON.parse(res.formdata)
                    let keys=Object.keys(this.defaultValue)
                    if (this.defaultValue && keys.includes('备注')) {
                        this.remark = this.defaultValue['备注'] || '';
                        delete this.defaultValue['备注']
                    }
                    console.log( 111,this.defaultValue)
                }
            }
        },
        async getWorkInfo(params) {
            let res = await getWorkInfo({
                wid: this.wid,
                mid: params.mid
            })
            this.workDetail = res
        },
        handClick(index) {
            this.current = index
            this.intoIndex = 'slot' + index
        },
        getForm(val) {
            this.form = val
        },
        submit(draft = 0) {
            wx.showModal({
                title: '提示',
                content: '是否确定提交全部工单和附件图片？',
                success: async (res) => {
                    if (res.confirm) {
                        console.log('用户点击确定', JSON.stringify(this.form))
                        let params = {
                            ...this.workInfo,
                            formdata: JSON.stringify({
                                ...this.form,
                                '备注': this.remark
                            }),
                            wid: this.wid,
                            draft
                        }
                        if (params.id == '0') {
                            delete params.id
                        }
                        await patrolAdd(params)
                        wx.showToast({
                            title: draft ? '保存至草稿成功' : '工单提交成功'
                        })
                        setTimeout(() => {
                            wx.navigateBack()
                        }, 1000)
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        }
    }
}
</script>

<style src="./assets/index.scss" lang="scss" scoped></style>


